﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Redirect404.Models;
using JetBrains.Annotations;
using Orchard.ContentManagement.Drivers;
using Orchard.Security;
using Orchard;
using Orchard.ContentManagement;

namespace Redirect404.Drivers
{
    [UsedImplicitly]
    public class RedirectPartDriver : ContentPartDriver<RedirectPart>
    {
        private readonly IAuthorizationService _authorizationService;
        private readonly IWorkContextAccessor _workContextAccessor;

        public RedirectPartDriver(IAuthorizationService authorizationService, IWorkContextAccessor workContextAccessor)
        {
            _authorizationService = authorizationService;
            _workContextAccessor = workContextAccessor;
        }

        protected override DriverResult Editor(RedirectPart itemPart, IUpdateModel updater, dynamic shapeHelper)
        {
            var currentUser = _workContextAccessor.GetContext().CurrentUser;
            if (!_authorizationService.TryCheckAccess(Permissions.ManageRedirects, currentUser, itemPart))
                return null;

            updater.TryUpdateModel(itemPart, Prefix, null, null);
            return null;
        }
    }
}